<?php
namespace App\Http\Controllers\Admin\Eticket;

use App\Http\Controllers\WechatController;
use App\Models\Eticket\Activity;
use App\Service\Eticket\ActivityService;
use Cache;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Service\Eticket\TicketService;
use App\Http\Requests\Eticket\ActivityRequest;
use Illuminate\Support\Facades\Redis;


/**
 * 电子门票活动控制器
 * Class ActivityController
 * @package App\Http\Controllers\Admin\Eticket
 *
 */
class ActivityController extends Controller
{
    private $ticket;
    private $activity;

    function __construct(ActivityService $activity, TicketService $ticket)
    {
        // 自定义活动中间件
        $this->middleware('check.permission:eticket/activity');
        $this->ticket = $ticket;
        $this->activity = $activity;
    }

    /**
     * 活动列表
     * @author 晚黎
     * @date   2016-10-29
     * @return [type]     [description]
     */
    public function index()
    {
        return view('admin.eticket.activity.list');
    }

    /**
     * datatables获取数据
     * @author 晚黎
     * @date   2016-10-29
     * @return [type]     [description]
     */
    public function ajaxIndex()
    {
        $responseData = $this->activity->ajaxIndex();
        return response()->json($responseData);
    }

    /**
     * 添加活动视图
     * @author 晚黎
     * @date   2016-11-01T16:47:26+0800
     * @return [type]                   [description]
     */
    public function create()
    {
        return view('admin.eticket.activity.create');
    }

    /**
     * 添加活动
     * @author 晚黎
     * @date   2016-11-02T10:30:30+0800
     * @param  ActivityRequest $request [description]
     * @return [type]                            [description]
     */
    public function store(ActivityRequest $request)
    {
        $this->activity->storeActivity($request->all());
        return redirect('admin/eticket/activity');
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function show($id)
    {
        $activity = $this->activity->editView($id);
        return view('admin.eticket.activity.edit')->with(compact('activity'));
    }

    /**
     * 修改活动视图
     * @author 晚黎
     * @date   2016-11-02T11:42:41+0800
     * @param  [type]                   $id [description]
     * @return [type]                       [description]
     */
    public function edit($id)
    {
        $activity = $this->activity->editView($id);
        return view('admin.eticket.activity.edit')->with(compact('activity'));
    }


    /**
     * 修改活动
     * @author 晚黎
     * @date   2016-11-02T11:58:45+0800
     * @param  TicketRequest $request [description]
     * @param  [type]                   $id      [description]
     * @return [type]                            [description]
     */
    public function update(ActivityRequest $request, $id)
    {
        $this->activity->updateActivity($request->all(), $id);
        return redirect('admin/eticket/activity');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $this->activity->destroyActivity($id);
        return redirect('admin/eticket/activity');
    }

    /**
     * 核销人员
     */
    public function writeOff($id)
    {
        $activity = $this->activity->editView($id);
        $writeOff['max'] = Cache::get(Activity::cacheWriteOffMaxKey($id), 0);
        list($writeOff['url'], $writeOff['qrcode']) = WechatController::qrcode(Activity::cacheWriteOffQrcodeKey($id),
            self::class, 'addWriteOffUser', [$id]);

        return view('admin.eticket.activity.writeOff')->with(compact('activity','writeOff'));
    }

    //更新核销人员最大人数
    public function updateWriteOffMax($id)
    {
        $max = request('max');
        if ($max && intval($max)>0) {
            Cache::forever(Activity::cacheWriteOffMaxKey($id), intval($max));
            return response()->json(['status'=>0]);
        }
        return response()->json(['status'=>1,'error'=>'参数错误']);
    }

    //获取核销人员
    public function getWriteOffUsers($id)
    {
        $responseData = $this->activity->getWriteOffUsers($id);
        return response()->json($responseData);
    }

    //扫码回调函数，增加核销人员
    public static function addWriteOffUser($id,$openid)
    {
        $activity = Activity::findOrFail($id);
        $count = Redis::scard(Activity::cacheWriteOffUserKey($id));
        $max = Cache::get(Activity::cacheWriteOffMaxKey($id), 0);
        if ($count < $max) {
            Redis::sAdd(Activity::cacheWriteOffUserKey($id),$openid);
            return '您已成为【' . $activity->name . '】活动的门票核销员';
        }
        return '核销人数人数已满';
    }
    //删除核销人员
    public function destroyWriteOffUser($id,$openid)
    {
        $result = Redis::sRem(Activity::cacheWriteOffUserKey($id),$openid);
        flash_info($result);
        return redirect(url('admin/eticket/activity/'.$id.'/writeOff'));
    }



    /**
     * 抽奖
     */
    public function draw()
    {

    }
}
